<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/12 0012
 * Time: 20:01
 */

namespace common\models\PlugsRunDesc;


use common\models\AgentInfo\AgentInfo;
use common\models\Plug\Plug;
use yii\helpers\ArrayHelper;

class PlugsRunDesc extends PlugsRunDescBase
{
    const TYPE_OK = 1;
    const TYPE_NO = 0;

    const SCENARIO_OPERATE = "operate";

    public function rules()
    {
        return array_merge([
            ["type", "default", "value" => self::TYPE_NO],
            ["type", "in", "range" => array_keys(self::getTypeMap()), "message" => "无效的类型"],
        ], parent::rules()); // TODO: Change the autogenerated stub
    }

    public static function getTypeMap($key = "ALL", $default = false)
    {
        $ret = [
            self::TYPE_OK => "有效",
            self::TYPE_NO => "无效"
        ];
        if ( $key === "ALL" ) {
            return $ret;
        }
        return ArrayHelper::getValue($ret, $key, $default);
    }

    public function scenarios()
    {
        return array_merge(parent::scenarios(), [
            self::SCENARIO_OPERATE => [
                "agent_id",
                "pcname",
                "adid",
                "type",
                "wbid",
                "city",
                "err",
                "mac",
            ]
        ]); // TODO: Change the autogenerated stub
    }

    public function getPlug()
    {
        return $this->hasOne(Plug::class, [
            "id" => "adid"
        ]);
    }
    public function getAgentInfo()
    {
        return $this->hasOne(AgentInfo::class, [
            "uid" => "agent_id"
        ]);
    }
    public function isCollect()
    {
        return $this->is_collect == self::IS_YES;
    }

    public function isCollectFail()
    {
        return $this->is_collect_fail == self::IS_YES;
    }

    public function isTypeOk()
    {
        return $this->type == self::TYPE_OK;
    }
}